package cn.jiyun.controller;

import cn.jiyun.service.IAddressService;
import cn.jiyun.model.pojo.Address;
import cn.jiyun.model.query.AddressQuery;
import cn.jiyun.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【-Address】操作接口
 *
 * @author
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/api/admin/address")
@Api(tags = "操作接口")
public class AddressController {

    @Autowired
    public IAddressService addressService;

    /**
     * 接口：添加
     * @param address 要添加的
     * @return Result 响应给前端
     */
    @PostMapping
    @ApiOperation("添加" )
    public Result save(@RequestBody Address address) {
        addressService.save(address);
        return Result.ok();
    }

    /**
     * 接口：根据Id删除
     * @param id 的Id
     * @return Result 响应给前端
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据Id删除")
    public Result remove(@PathVariable("id") Long id) {
        addressService.delete(id);
        return Result.ok();
    }

    /**
     * 接口：修改
     * @param address 要修改的数据
     * @return Result 响应给前端
     */
    @PutMapping("update")
    @ApiOperation("修改")
    public Result updateById(@RequestBody Address address) {
        addressService.updateById(address);
        return Result.ok();
    }


    /**
      * 根据Id查询单个
     */
//    @GetMapping("/get/{id}")
//    @ApiOperation("根据Id查询单个")
//    public Result getById(@PathVariable Long id)  {
//        Address one = addressService.getById(id);
//        return Result.ok(one);
//    }

    /**
     * 接口：查询所有
     */
    @GetMapping("/{id}")
    @ApiOperation("查询所有")
    public Result getList(@PathVariable Integer id) {
        List<Address> listAll = addressService.showlist(id);
        return Result.ok(listAll);
    }
    @GetMapping("/defeaul/{id}")
    @ApiOperation("查询默认地址")
    public Result getDefaultAddress(@PathVariable Integer id) {
        Address defaultAddress = addressService.getDefaultAddress(id);
        return Result.ok(defaultAddress);
    }
    @GetMapping("/updatedefault/{id}/{addressId}")
    @ApiOperation("设置默认地址")
    public Result updateDefaultAddress(@PathVariable Integer id,@PathVariable Long addressId) {
        addressService.updateDefaultAddress(id,addressId);
        return Result.ok();
    }


    /**
     * 接口：分页查询所有
     * @return List<Address> 所有数据
     */
//    @GetMapping("/{page}/{limit}")
//    @ApiOperation("获取所有")
//    public Result getPageList(@PathVariable Integer page, @PathVariable Integer limit, AddressQuery query) {
//        //封装分页参数
//        query.setCurrentPage(page);
//        query.setPageSize(limit);
//        //查询分页数据
//        IPage<Address> pageList = addressService.getPageList(query);
//        return Result.ok(pageList);
//    }

}
